const bookTable = require('../tables/books');
const seq = require('../config/database').seq;
const attributes = ['id', 'bookno', 'bookname', 'bookauthor', 'bookstatus', 'booktype', 'booksrc', 'bookimg', 'booknotice', 'capturehref', 'sourcesite']
let modelObj = {
	getBookNo(callback) {
		seq.query('CALL generateBookNo()', { type: seq.QueryTypes.SELECT }).then(data => {
			let bkno = JSON.parse(JSON.stringify(data[0][0])).newBookNo;
			callback(bkno);
		});
	},
	getBookList(page, limit, callback) {
		let offset = (page - 1) * limit;
		bookTable.findAndCountAll({ offset, limit, attributes }).then(data => {
			callback(data);
		})
	},
	getBookOne(id, callback) {
		bookTable.findOne({ where: { id }, attributes }).then(data => {
			callback(JSON.stringify(data));
		})
	},
	//获取书名
	getBookName(bookno, callback) {
		let attributes = ['bookno', 'bookname', 'booksrc', 'capturehref', 'sourcesite'];
		bookTable.findOne({ where: { bookno }, attributes }).then(data => {
			callback(JSON.stringify(data));
		})
	},
	createBook(params, callback) {
		bookTable.create(params).then(data => {
			callback(true);
		}).catch(err => {
			callback(false);
		})
	},
	updateBook(id, params, callback) {
		bookTable.update(params, { where: { id } }).then(data => {
			callback(data);
		});
	},
	deleteBook(id, callback) {
		bookTable.destroy({ where: { id } }).then(data => {
			callback(data);
		})
	}
}
module.exports = modelObj;